using ICSharpCode.TextEditor.Document;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
namespace ICSharpCode.TextEditor.Util
{
	public class RtfWriter
	{
		private static Dictionary<string, int> colors;
		private static int colorNum;
		private static StringBuilder colorString;
		public static string GenerateRtf(TextArea textArea)
		{
			RtfWriter.colors = new Dictionary<string, int>();
			RtfWriter.colorNum = 0;
			RtfWriter.colorString = new StringBuilder();
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1031");
			RtfWriter.BuildFontTable(textArea.Document, stringBuilder);
			stringBuilder.Append('\n');
			string value = RtfWriter.BuildFileContent(textArea);
			RtfWriter.BuildColorTable(textArea.Document, stringBuilder);
			stringBuilder.Append('\n');
			stringBuilder.Append("\\viewkind4\\uc1\\pard");
			stringBuilder.Append(value);
			stringBuilder.Append("}");
			return stringBuilder.ToString();
		}
		private static void BuildColorTable(IDocument doc, StringBuilder rtf)
		{
			rtf.Append("{\\colortbl ;");
			rtf.Append(RtfWriter.colorString.ToString());
			rtf.Append("}");
		}
		private static void BuildFontTable(IDocument doc, StringBuilder rtf)
		{
			rtf.Append("{\\fonttbl");
			rtf.Append("{\\f0\\fmodern\\fprq1\\fcharset0 " + doc.TextEditorProperties.Font.Name + ";}");
			rtf.Append("}");
		}
		private static string BuildFileContent(TextArea textArea)
		{
			StringBuilder stringBuilder = new StringBuilder();
			bool flag = true;
			Color right = Color.Black;
			bool flag2 = false;
			bool flag3 = false;
			bool flag4 = false;
			foreach (ISelection current in textArea.SelectionManager.SelectionCollection)
			{
				int num = textArea.Document.PositionToOffset(current.StartPosition);
				int num2 = textArea.Document.PositionToOffset(current.EndPosition);
				for (int i = current.StartPosition.Y; i <= current.EndPosition.Y; i++)
				{
					LineSegment lineSegment = textArea.Document.GetLineSegment(i);
					int num3 = lineSegment.Offset;
					if (lineSegment.Words != null)
					{
						foreach (TextWord current2 in lineSegment.Words)
						{
							switch (current2.Type)
							{
								case TextWordType.Word:
								{
									Color color = current2.Color;
									if (num3 + current2.Word.Length > num && num3 < num2)
									{
										string key = string.Concat(new object[]
										{
											color.R, 
											", ", 
											color.G, 
											", ", 
											color.B
										});
										if (!RtfWriter.colors.ContainsKey(key))
										{
											RtfWriter.colors[key] = ++RtfWriter.colorNum;
											RtfWriter.colorString.Append(string.Concat(new object[]
											{
												"\\red", 
												color.R, 
												"\\green", 
												color.G, 
												"\\blue", 
												color.B, 
												";"
											}));
										}
										if (color != right || flag)
										{
											stringBuilder.Append("\\cf" + RtfWriter.colors[key].ToString());
											right = color;
											flag4 = true;
										}
										if (flag2 != current2.Italic)
										{
											if (current2.Italic)
											{
												stringBuilder.Append("\\i");
											}
											else
											{
												stringBuilder.Append("\\i0");
											}
											flag2 = current2.Italic;
											flag4 = true;
										}
										if (flag3 != current2.Bold)
										{
											if (current2.Bold)
											{
												stringBuilder.Append("\\b");
											}
											else
											{
												stringBuilder.Append("\\b0");
											}
											flag3 = current2.Bold;
											flag4 = true;
										}
										if (flag)
										{
											stringBuilder.Append("\\f0\\fs" + textArea.TextEditorProperties.Font.Size * 2f);
											flag = false;
										}
										if (flag4)
										{
											stringBuilder.Append(' ');
											flag4 = false;
										}
										string text;
										if (num3 < num)
										{
											text = current2.Word.Substring(num - num3);
										}
										else
										{
											if (num3 + current2.Word.Length > num2)
											{
												text = current2.Word.Substring(0, num3 + current2.Word.Length - num2);
											}
											else
											{
												text = current2.Word;
											}
										}
										RtfWriter.AppendText(stringBuilder, text);
									}
									num3 += current2.Length;
									break;
								}
								case TextWordType.Space:
								{
									if (current.ContainsOffset(num3))
									{
										stringBuilder.Append(' ');
									}
									num3++;
									break;
								}
								case TextWordType.Tab:
								{
									if (current.ContainsOffset(num3))
									{
										stringBuilder.Append("\\tab");
									}
									num3++;
									flag4 = true;
									break;
								}
							}
						}
						if (num3 < num2)
						{
							stringBuilder.Append("\\par");
						}
						stringBuilder.Append('\n');
					}
				}
			}
			return stringBuilder.ToString();
		}
		private static void AppendText(StringBuilder rtfOutput, string text)
		{
			for (int i = 0; i < text.Length; i++)
			{
				char c = text[i];
				char c2 = c;
				if (c2 != '\\')
				{
					switch (c2)
					{
						case '{':
						{
							rtfOutput.Append("\\{");
							goto IL_8C;
						}
						case '}':
						{
							rtfOutput.Append("\\}");
							goto IL_8C;
						}
					}
					if (c < 'Ā')
					{
						rtfOutput.Append(c);
					}
					else
					{
						rtfOutput.Append("\\u" + ((short)c).ToString() + "?");
					}
				}
				else
				{
					rtfOutput.Append("\\\\");
				}
				IL_8C:;
			}
		}
	}
}
